<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-form ref="formRef" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="Banner" prop="image">
        <el-upload
          accept="image/png, image/jpeg"
          :action="uploadActionUrl"
          :on-success="handleSuccess"
        >
          <el-button v-if="!imgpath" size="small" type="primary">
            点击上传
          </el-button>
          <img
            v-if="imgpath"
            :src="imgpath"
            style="max-width: 250px; max-height: 250px"
          />
        </el-upload>
      </el-form-item>
      <!-- <el-form-item label="链接地址" prop="link">
        <el-input v-model.trim="form.link" />
      </el-form-item> -->
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { defineComponent, getCurrentInstance, reactive, toRefs } from 'vue'
  import { doEdit } from '@/api/banner'
  import { baseURL, HOST } from '@/config'

  export default defineComponent({
    name: 'BannerEdit',
    emits: ['fetch-data'],
    // 处于生命周期函数 beforeCreate 和 Created 两个钩子函数之前的函数
    // 执行 setup 时，组件实例尚未被创建（在 setup() 内部，this 不会是该活跃实例的引用，即不指向vue实例，Vue 为了避免我们错误的使用，直接将 setup函数中的this修改成了 undefined）
    setup(props, { emit }) {
      const { proxy } = getCurrentInstance()
      console.log(window.location.host)
      const state = reactive({
        formRef: null,
        imgpath: '',
        uploadActionUrl: baseURL + 'upload/ajaxUploads',
        form: {
          id: '',
          image: '',
        },
        rules: {
          image: [{ required: true, trigger: 'blur', message: '请上传Bnaner' }],
        },
        title: '',
        dialogFormVisible: false,
        roleList: [],
      })
      const showEdit = (row) => {
        if (row.id) {
          state.title = '编辑'
          state.form = Object.assign({}, row)
          state.imgpath = state.form.image ? HOST + state.form.image : ''
        } else {
          state.title = '添加'
          state.form = Object.assign({}, row)
        }
        state.dialogFormVisible = true
      }
      const close = () => {
        state['formRef'].resetFields()
        state.imgpath = ''
        state.dialogFormVisible = false
      }
      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { msg } = await doEdit(state.form)
            proxy.$baseMessage(msg, 'success', 'vab-hey-message-success')
            emit('fetch-data')
            close()
          }
        })
      }
      const handleSuccess = (response) => {
        if (response.code == 0) {
          state.form.image = response.data[0].path
          state.imgpath = HOST + response.data[0].path
        }
      }

      // 与模板一起使用：需要返回一个对象 (在setup函数中定义的变量和方法最后都是需要 return 出去的 不然无法再模板中使用)
      return {
        ...toRefs(state),
        showEdit,
        close,
        save,
        handleSuccess,
      }
    },
  })
</script>
<style lang="scss" scoped>
  .tip {
    margin: 0px;
    color: red;
  }
  :deep() {
    .el-upload-list {
      display: none;
    }
  }
</style>
